// ==UserScript==
// @name         JS学习
// @namespace    http://tampermonkey.net/
// @version      0.1.20220516
// @description  仅用于学习使用，网课自动挂机看视频，网课作业自动做题共享答案。网课在线辅助脚本❤网课辅助题库❤全自动化操作挂机【易全题库】
// @author       YanMing
// @match        *://www.baidu.com/*
// @match        *://*.uooconline.com/*
// @icon         
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // 设置修改后，需要刷新或重新打开网课页面才会生效 设置了响应时间，更新再等等吧
    var SETTING = {
        time: 10, // 默认响应速度为10秒，不建议小于5秒。
        mode: 0, // 0:队列模式(开启后任务点逐一完成)，1:复习模式(支持挂机任务点已完成的视频和音频)，默认队列模式。
        task: 0, // 0:所有自动执行,1:音频自动播放，2:图书自动阅读，3:文档自动阅读，默认所有自动执行。
        answer: 0, // 0:开启自动答题功能(章节测验)，1:关闭。作业需要手动开启查询，高准确率，默认开启。
        none: 0, // 0:开启默认答题，1:关闭， 开启后无匹配答案时执行默认答案，关闭后若题目无匹配答案则会暂时保存已作答的题目，默认开启。
        read: 65, // 挂机课程阅读时间，单位是分钟，'65'代表挂机65分钟，请手动打开阅读页面，默认'65'分钟。
        face: 1, // 解除面部识别(不支持二维码类面部采集)，此功能仅为临时解除，默认开启。
        total: 1, // 显示课程进度的统计数据，在学习进度页面的上方展示，默认开启。
        vol: 0, // 默认音量的百分数，设定范围：[0,100]，'0'为静音，默认'0'。
        rate: 0, // 视频播放默认倍率，参数范围[1,5]，默认'1'。
        platform: '',// 当前平台，程序自动赋值，不需要设置
        api: 'http://localhost:8080'
    };
    // start
    auto_init();
    function auto_init(){
        startConsole();
        // 判断当前是哪个平台
        var thatHost = document.location.host;
        if(thatHost){
            var hostArr=thatHost.split(".");
            if(hostArr && hostArr.length>0){
                SETTING.platform = hostArr[hostArr.length-2] ;
            }
        }
        allow_platform();
    }
    // 输出
    function startConsole() {
        const str =
            "易全题库: \n" +
            "每一个星球都有一个驱动核心，每一种思想都有影响力的种子。\n";
        const ok =
            " ......................阿弥陀佛......................\n" +
            "                       _oo0oo_                      \n" +
            "                      o8888888o                     \n" +
            '                      88" . "88                     \n' +
            "                      (| -_- |)                     \n" +
            "                      0\\  =  /0                     \n" +
            "                   ___/‘---’\\___                   \n" +
            "                  .' \\|       |/ '.                 \n" +
            "                 / \\\\|||  :  |||// \\                \n" +
            "                / _||||| -卍-|||||_ \\               \n" +
            "               |   | \\\\\\  -  /// |   |              \n" +
            "               | \\_|  ''\\---/''  |_/ |              \n" +
            "               \\  .-\\__  '-'  ___/-. /              \n" +
            "             ___'. .'  /--.--\\  '. .'___            \n" +
            '         ."" ‘<  ‘.___\\_<|>_/___.’>’ "".          \n' +
            "       | | :  ‘- \\‘.;‘\\ _ /’;.’/ - ’ : | |        \n" +
            "         \\  \\ ‘_.   \\_ __\\ /__ _/   .-’ /  /        \n" +
            "    =====‘-.____‘.___ \\_____/___.-’___.-’=====     \n" +
            "                       ‘=---=’                      \n" +
            "                                                    \n" +
            "...........佛祖保佑     永不挂科     考试必过..........";


        console.log( str + "\n" + ok);
    };

    function allow_platform(){
        var platform = {
            baidu: { name: "baidu联盟", match: "*://*.baidu.com/*" },
            uooconline: { name: "UOOC优课联盟", match: "*://*.uooconline.com/*" },
        };
        if(SETTING.platform){
            var thatPlatform=platform[SETTING.platform];
            if(thatPlatform){
                console.log("当前正在使用"+thatPlatform.name+"！")
                switch(SETTING.platform) {
                    case "uooconline":
                        uooc_start();
                        break;
                    default:
                        console.warn(thatPlatform.name+"，暂未实现！")
                }
            }else{
                console.warn("当前暂不支持"+SETTING.platform+"平台！")
            }
        }
    }



    // uooconline  start ...
    var UoocAutoLearn ={
        index:"",
        course_id: "",
        course_name: "",// 课程名称
        chapter_id: 0, // 当前章节
        section_id: "", // 内容部分
        resource_id: "", // 资源
        catalog_id: 0,
        task_id: 0,// 任务ID，考试ID
        video_pos:0,
        video_length:0,
        video_play_source: "",
        title: "",// 视频标题 XXX.mp4
        subsection_id: "",
        parent_name: "" // 课程信息
    }
    function uooc_start(){
        uooc_change_page();
        uooc_push_course_list();
        // 绑定按钮事件
        $(document).on('click', '.uooc-auto-learn-btn', function () {
            var courseId = this.dataset.cid;
            var courseName = this.dataset.cname;
            var index = this.dataset.index;
            if(courseId){
                console.log('开始任务在线挂机任务，'+courseName+"["+courseId+"]");
                UoocAutoLearn.index = index;
                UoocAutoLearn.course_id = courseId;
                UoocAutoLearn.course_name = courseName;
                uooc_course_learn_progress ();
            }
        });
    }
    function uooc_change_page(){
        if (/^\/home\/learn\/index/.test(location.pathname)) {
            //课程观看视频页（课程号+章节号） section
            var thatUrl =  window.location.href;
            if( thatUrl.indexOf("#")>0) {
                var spstr = thatUrl.split("#")[1];
                //console.warn("["+spstr+"]");
                var idArr = spstr.split("/");
                var last = idArr[idArr.length-1];
                let dataLearn = {
                    cid: idArr[1],//课程ID
                    chapter_id: idArr[2],//章节ID(一级目录ID)
                    section_id: idArr[3],//内容部分ID(二级目录ID)
                    subsection_id: '',//内容部分ID(三级目录ID)
                    catalog_id: ''//当前目录ID
                }
                let learn_id = '';
                if(last == "section"){
                    dataLearn.catalog_id = idArr[3]
                    learn_id = idArr[4]
                }else  if(last == "subsection"){
                    dataLearn.subsection_id = idArr[4]
                    dataLearn.catalog_id = idArr[4]
                    learn_id = idArr[5]
                }

                $.ajax({
                    type: "GET",
                    url: '/home/learn/getUnitLearn',
                    data: dataLearn,
                    success: function (response) {
                        if (response.code == 1 && response.data && response.data.length > 0) {
                            var data = response.data;
                            for(var i=0;i<data.length;i++){
                                if(data[i].id == learn_id && data[i].task_id){
                                    if(data[i].finished==0){
                                        // 还没做作业
                                        console.log('开始做作业', dataLearn.cid, data[i].task_id);
                                    }else if(data[i].finished==1){
                                        // 已经完成作业了
                                        console.log('同步作业', dataLearn.cid, data[i].task_id);
                                    }

                                }
                            }
                        }
                    }
                })
            }
            return;
        } else if (/^\/home/.test(location.pathname)) {
            ///home#/center/course/learn
            var thatUrl =  window.location.href;
            if( thatUrl.indexOf("#")>0){
                var spstr = thatUrl.split("#")[1];
                if(spstr =="/center/course/learn"){
                    //开课中
                    console.log("尝试在开课中的课程，添加'在线挂机'按钮");
                    // 延后执行
                    setTimeout(() => {
                        uooc_home_add_btn()
                    }, 1000);
                }else if(spstr =="/center/course/finish"){
                    //已结束，同步答案
                }else{
                    console.warn("暂不支持["+spstr+"]");
                }
            }
            // 尝试添加按钮
            //if (/^\/home\/course\/[0-9]+/.test(location.pathname)) {
            console.log("尝试添加home按钮");
            return;
        }else if (/^\/exam\/[0-9]+/.test(location.pathname)) {
            // 考试 /exam/955957832 做题页面
            // 判断题目是否出来
            if ($('.ti-q-c').length > 0) {
                console.log("尝试添加exam按钮");

            }
            return;
        }else if (/^\/exam\/paper/.test(location.pathname)) {
            // 作业 /exam/paper?cid=110189145&tid=256749451
            // 判断题目是否出来
            uooc_task_exam()
            return;
        }else{

            console.warn("暂不支持 "+location.pathname)
            return;
        }
    }

    // 遍历添加按钮
    function uooc_home_add_btn(){
        var courseArr = $('.course-item .course-right-bottom-btn').not('.uooc-auto-learn-btn');
        if(courseArr && courseArr.length>0){
            $('.course-item .course-right-bottom-btn').not('.uooc-auto-learn-btn').each(function (index, item) {
                // 设置未修改过的
                if (typeof item.dataset.btnAdd == "undefined") {
                    // http://cce.org.uooconline.com/home/course/课程ID号
                    var courseId = item.href.substr(item.href.lastIndexOf("/")+1);
                    if(!courseId){
                        courseId = item.pathname.split('/').pop();
                    }

                    var courseName = $('.course-name')[index].innerText;
                    var courseTeacher = $('.course-teacher')[0].innerText;
                    console.log(courseName+"["+courseId+"] "+courseTeacher+",课程添加“在线挂机”按钮");
                    if (courseId) {
                        // 按钮
                        var btnHtml = '<a class="course-right-bottom-btn uooc-auto-learn-btn" style="font-size: 12px; width: 58px; margin-left: 4px;" data-index="' + index + '" data-cid="' + courseId + '" data-cname="' + courseName + '">在线挂机</a>';
                        // 设置为已修改
                        item.dataset.btnAdd = 'isAdd';
                        // 修改样式
                        item.style.fontSize = '12px';
                        item.style.width = '58px';
                        // 追加元素
                        $(item).before(btnHtml);
                    }
                }
            });
        }else{
            // 定时查询
        }

    }

    // 获取课程进度
    function uooc_course_learn_progress (){
        $.ajax({
            type: "GET",
            url: '/home/learn/getCourseLearn',
            data: {
                cid: UoocAutoLearn.course_id
            },
            success: function (response) {
                if (response.code !== 1) {
                    console.log('Error', response);
                    return;
                }
                UoocAutoLearn.chapter_id = response.data.chapter_id;
                UoocAutoLearn.section_id = response.data.section_id;
                UoocAutoLearn.resource_id = response.data.resource_id;
                UoocAutoLearn.catalog_id = response.data.catalog_id;
                UoocAutoLearn.subsection_id = response.data.subsection_id;
                UoocAutoLearn.parent_name = response.data.parent_name;
                console.log(
                    '课程信息', UoocAutoLearn.parent_name,
                    '章节ID', UoocAutoLearn.chapter_id,
                    '内容部分ID', UoocAutoLearn.section_id,
                    '资源ID', UoocAutoLearn.resource_id
                );
                // 如果没有看过
                if (UoocAutoLearn.chapter_id <= 0) {
                    console.log("开始挂机")
                    // 获取章节信息
                    uooc_course_catalog_list()
                }else{
                    // 看过章节，获取课程观看时间（进度）
                    uooc_course_catalog_unit_learn()
                }

            }
        });

    }
    // 获取课程章节列表
    function uooc_course_catalog_list(){
        $.ajax({
            type: "GET",
            url: '/home/learn/getCatalogList',
            data: {
                cid: UoocAutoLearn.course_id
            },
            success: function (response) {
                //console.log(response.data);
                // { desc: "", finished: 1, id: 2137386916, is_pay: "0", learn_mode: "10", learn_task_cnt: "20", learning: 0, name: "第X章 XX", order_id: "70000", parent: 0, pid: 0, status: "2", task_id: 0 }
                if (response.code !== 1) {
                    console.log('Error', response);
                    return;
                }
                uooc_course_catalog_loop(response.data)
            }
        });
    };
    //  遍历课程章节，查看每个章节是否完成
    function uooc_course_catalog_loop(data){
        var isFinished = true;
        for (let index = 0; index < data.length; index++) {
            const item = data[index];
            if (item.finished == 0) {
                isFinished = false;
                if (typeof item.children != 'undefined') {
                    // 看当前目录是否有视频？？？？？
                    // 有二级目录，进行遍历，根目录有需要学习的,播放这个课程
                    uooc_course_catalog_loop(item.children);
                    break;
                } else {
                    // 播放这个课程
                    console.log('新的章节',item.name, item.id, item.pid );
                    UoocAutoLearn.catalog_id = item.id;
                    UoocAutoLearn.chapter_id = item.pid;
                    UoocAutoLearn.video_pos = 0;
                    // 开始下一课程
                    uooc_course_catalog_unit_learn();
                    break;
                }
            }
        }
        if (isFinished) {
            console.log("恭喜，"+UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"]课程已全看完。");
        }
    };

    // 获看过章节，获取课程观看时间（进度）
    function uooc_course_catalog_unit_learn(){
        //console.log('/home/learn/getUnitLearn?cid='+UoocAutoLearn.course_id+'&chapter_id='+UoocAutoLearn.chapter_id+'&section_id='+UoocAutoLearn.section_id+'&catalog_id='+UoocAutoLearn.catalog_id)
        $.ajax({
            type: "GET",
            url: '/home/learn/getUnitLearn',
            data: {
                cid: UoocAutoLearn.course_id,
                chapter_id: UoocAutoLearn.chapter_id,
                section_id: UoocAutoLearn.section_id,
                catalog_id: UoocAutoLearn.catalog_id
            },
            success: function (response) {
                if (response.code !== 1) {
                    console.log('Error', response);
                    return;
                }

                if(response.data && response.data.length>0){
                    // 遍历每一个视频
                    var isFinished = true;
                    for (let index = 0; index < response.data.length; index++) {
                        const item = response.data[index];
                        if (item.finished == 0) {
                            if(item.type == 10 && item.video_play_list && item.video_play_list.length>0){
                                // 是视频，继续播放，还未看完
                                UoocAutoLearn.video_pos = parseFloat(item.video_pos);
                                UoocAutoLearn.video_play_source = item.video_play_list[0].source;
                                UoocAutoLearn.title = item.title;
                                UoocAutoLearn.resource_id = item.id;
                                isFinished = false;
                                // 获取视频时长
                                uooc_course_catalog_video_length()
                                break;
                            }else if( item.type == 60 && item.content && item.h5_url){
                                // 文本任务跳过，不处理
                                continue;
                            }else if(item.type == 80 && item.task_id){
                                // 考试
                                isFinished = false;
                                console.warn("需要进行章节测验")
                                UoocAutoLearn.task_id = item.task_id;
                                let dataLearn = {
                                    cid: UoocAutoLearn.course_id,//课程ID
                                    cname: UoocAutoLearn.course_name,//课程名称
                                    chapter_id: UoocAutoLearn.chapter_id,//章节ID(一级目录ID)
                                    chapter_name: '',//章节名称
                                    section_id: UoocAutoLearn.section_id,//内容部分ID(二级目录ID)
                                    section_name: '',//内容部分名称
                                    subsection_id: UoocAutoLearn.subsection_id,//内容部分ID(三级目录ID)
                                    subsection_name: '',//内容部分名称
                                    catalog_id: UoocAutoLearn.catalog_id,//当前目录ID
                                    task_id: UoocAutoLearn.task_id// 任务ID
                                }
                                uooc_task_exam_view (dataLearn)
                                //
                                uooc_course_catalog_exam_paper()
                                //console.warn("请设置浏览器允许弹出窗体！")
                                //window.open('/home/learn/index#/'+item.course_id+'/'+UoocAutoLearn.chapter_id+'/'+UoocAutoLearn.section_id+'/'+item.catalog_id+'/'+item.id+'/subsection','_blank')
                                break;
                            }else{
                                // 其他非视频，新页面显示
                                // window.open('/home/learn/index#/'+item.course_id+'/'+UoocAutoLearn.chapter_id+'/'+UoocAutoLearn.section_id+'/'+item.catalog_id+'/'+item.id+'/subsection','_blank');
                                continue;
                            }

                        }
                    }
                }else{
                    isFinished = false;
                }


                // 如果都看完了
                if (isFinished) {
                    // 获取下一节课
                    uooc_course_catalog_list();
                }
            }
        });
    }
    // 获取视频长度
    function uooc_course_catalog_video_length () {
        var obj = $('.course-cover')[UoocAutoLearn.index];
        var video = document.createElement("video");
        //video.setAttribute("width", "320");
        //video.setAttribute("height", "240");
        video.setAttribute("controls", "controls");
        video.setAttribute("src", UoocAutoLearn.video_play_source);
        obj.appendChild(video);
        // 加载完成后调用
        //video.onloadedmetadata = function() {
        video.onloadeddata = function () {
            //console.log(video.duration); //打印时长
            UoocAutoLearn.video_length = video.duration;
        }
        //console.log(UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"], "+ UoocAutoLearn.title+' ===> 总时长'+UoocAutoLearn.video_length+'秒, 已看至'+ UoocAutoLearn.video_pos+'秒');
        // 开始刷新时间,每X秒提交一次
        uooc_course_catalog_video_mark();
        return;
    };


    // 提交课程观看进度，刷新时间
    function uooc_course_catalog_video_mark () {
        UoocAutoLearn.video_pos = UoocAutoLearn.video_pos + SETTING.time;

        if (UoocAutoLearn.video_pos >= UoocAutoLearn.video_length && UoocAutoLearn.video_length > 0) {
            UoocAutoLearn.video_pos = UoocAutoLearn.video_length;
        }
        $.ajax({
            type: "POST",
            url: '/home/learn/markVideoLearn',
            data: {
                chapter_id: UoocAutoLearn.chapter_id,
                cid: UoocAutoLearn.course_id,
                // hidemsg_: true,
                network: 3,
                resource_id: UoocAutoLearn.resource_id,
                section_id: UoocAutoLearn.section_id,
                source: 1,
                subsection_id: UoocAutoLearn.subsection_id,
                video_length: UoocAutoLearn.video_length,
                video_pos: UoocAutoLearn.video_pos
            },
            success: function (response) {
                if (response.code !== 1) {
                    console.log('Error', response);
                    console.warn("出现异常，暂停任务。",response.msg,UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"], "+ UoocAutoLearn.title+"["+UoocAutoLearn.chapter_id+"]");
                    return;
                }

                console.log(UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"], "+ UoocAutoLearn.title+"["+UoocAutoLearn.chapter_id+"] ===> 已看至", UoocAutoLearn.video_pos, '秒, 总', UoocAutoLearn.video_length == 0 ? '未知' : UoocAutoLearn.video_length, '秒');
                if (response.data.finished == 1 || (UoocAutoLearn.video_length > 0 && UoocAutoLearn.video_pos >= UoocAutoLearn.video_length)) {
                    console.log(UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"]本课已经结束");
                    // 获取下一节课
                    uooc_course_catalog_list();
                    return;
                }
                // 开始刷新时间,X秒后再提交一次
                setTimeout(() => {
                    uooc_course_catalog_video_mark();
                }, SETTING.time * 1000);

            }
        });
    };
    // 获取章节试题
    function uooc_course_catalog_exam_paper () {
        $.ajax({
            type: "GET",
            url: '/exam/getTaskPaper',
            data: {
                tid: UoocAutoLearn.task_id,
            },
            success: function (response) {
                if (response.code !== 1) {
                    console.log('Error', response);
                    console.warn("出现异常，暂停任务。",response.msg,UoocAutoLearn.course_name+"["+UoocAutoLearn.course_id+"], "+ UoocAutoLearn.title+"["+UoocAutoLearn.chapter_id+"]");
                    return;
                }
                //console.log(response.data)
                var questions = response.data.questions;
                if(questions && questions.length > 0) {
                    questions.forEach(function(question, index) {
                        if(question){
                            if(question.type == "10"){
                                // 题型 type：10，type_text: "单选题"
                                // 难度 level_text	易、中、难
                                // 问题 question
                                // 选项 options   或 options_app
                                // 学生答案 student_answer
                                const regex = /<\/?.+?\/?>/gm
                                const subst = ``;
                                const result = question.question.replace(regex, subst);
                                console.log("单选题：",result)
                                console.log("A：",question.options.A)
                                console.log("B：",question.options.B)
                                console.log("C：",question.options.C)
                                console.log("D：",question.options.D)
                            }
                        }
                    });
                }
            }
        });

    }

    function uooc_task_exam_view (dataLearn) {
        $.ajax({
            type: "GET",
            url: '/exam/view',
            data: {
                cid: dataLearn.cid,
                tid: dataLearn.task_id
            },
            success: function (response) {
                // 判断是否提交试卷
                if (response.code !== 1) {
                    console.log('Error', response);
                    console.warn("出现异常，暂停任务。",response.msg,dataLearn.cname+"["+dataLearn.cid+"], "+ dataLearn.chapter_name+"["+dataLearn.chapter_id+"]");
                    return;
                }else if (response.code == 1) {
                    uooc_push_task_exam (dataLearn,response.data)
                    /**
                    var questions = response.data.questions;
                    if(questions && questions.length > 0) {
                        questions.forEach(function(question, index) {
                            if(question){
                                const regex = /<\/?.+?\/?>/gm
                                const subst = ``;
                                const result = question.question.replace(regex, subst);
                                if(question.type == "10"){
                                    console.log("单选题：",result)
                                }else if(question.type == "11"){
                                    console.log("多选题：",result)
                                }else if(question.type == "20"){
                                    console.log("判断题：",result)
                                }
                            }
                        });
                    }
                     **/


                }
            }
        });
    }

    // 获取作业试题
    function uooc_task_exam () {
        var cid='';
        var tid='';
        var result = urlArgs();
        for (var key in result) {
            switch (key){
                case 'cid':
                    cid = result[key]
                case 'tid':
                    tid = result[key]
            }
        }
        //uooc_task_exam_view (cid, tid)
    }

    function uooc_push_task_exam (dataLearn,examData) {
        examData.task.course_id =  dataLearn.cid;
        examData.task.chapter_id = dataLearn.chapter_id;
        examData.task.section_id = dataLearn.section_id;
        examData.task.subsection_id = dataLearn.subsection_id
        examData.task.task_id = dataLearn.task_id;

        // 提交试卷到服务器
        $.ajax({
            type: "POST",
            url: SETTING.api+'/uooc/exam/view',
            dateType: "json",
            contentType: "application/json",
            data: JSON.stringify(examData),
            success: function (response) {

            }
        });
    }
    // 获取课程列表
    function uooc_push_course_list(){
        // 已学完的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=finish
        setTimeout(() => {
            uooc_push_course_type('finish')
        }, 1000);
        // 正在学的课程https://cce.org.uooconline.com/home/course/list?keyword=&page=1&type=learn
        // 延后执行
        setTimeout(() => {
            uooc_push_course_type('learn')
        }, 3000);

    }

    function uooc_push_course_type(type){
        if(type == "learn" || type == "finish" ){
            $.ajax({
                type: "GET",
                url: '/home/course/list',
                data: {
                    page: 1,
                    type: type,
                    keyword: ''
                },
                success: function (response) {
                    // 判断是否提交试卷
                    if (response.code == 1) {
                        if(response.data && response.data.data && response.data.data.length > 0) {
                            uooc_push_course_chapter_exam(response.data.data)
                        }
                    }
                }
            });
        }

    }

    function uooc_push_course_info(cid){
        $.ajax({
            type: "GET",
            url: '/home/learn/getCourseLearn',
            data: {
                cid: cid
            },
            success: function (response) {
                if (response.code == 1 && response.data) {
                    var courseInfo = {
                        course_id: cid,
                        course_name:response.data.parent_name,
                        teacher_name:response.data.teacher_name,
                        org_name:response.data.org_name
                    }
                    // 发送请求
                    $.ajax({
                        type: "POST",
                        url: SETTING.api+'/uooc/course',
                        dateType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(courseInfo),
                        success: function (response) {

                        }
                    });
                }
            }
        });
    }
    function uooc_push_course_catalog(courseId, catalogData){
        $.ajax({
            type: "POST",
            url: SETTING.api+'/uooc/course/catalog',
            dateType: "json",
            contentType: "application/json",
            data: JSON.stringify({courseId:courseId,courseCatalogList: catalogData}),
            success: function (response) {

            }
        });

    }
    function uooc_push_course_chapter_exam(courseData){
        if(courseData && courseData.length>0){
            for(var i=0;i<courseData.length;i++){
                var cid = courseData[i].id;
                var cname = courseData[i].parent_name;
                // 延后执行
                setTimeout(() => {
                    uooc_push_course_info(cid)
                }, 100);
                // 延后执行
                setTimeout(() => {
                    $.ajax({
                        type: "GET",
                        url: '/home/learn/getCatalogList',
                        data: {
                            cid: cid
                        },
                        success: function (response) {
                            if (response.code == 1) {
                                uooc_push_course_catalog(cid, response.data)
                                uooc_push_course_chapter_exam_loop(cid,cname,response.data)
                            }
                        }
                    });
                }, 100);

            }
        }
    }

    function uooc_push_course_chapter_exam_loop(cid, cname, data){
        let dataLearn = {
            cid: cid,//课程ID
            cname: cname,//课程名称
            chapter_id: '',//章节ID(一级目录ID)
            chapter_name: '',//章节名称
            section_id: '',//内容部分ID(二级目录ID)
            section_name: '',//内容部分名称
            subsection_id: '',//内容部分ID(三级目录ID)
            subsection_name: '',//内容部分名称
            catalog_id: '',//当前目录ID
            task_id: ''// 任务ID
        }

        for(var chapter of data){
            // 章节
            dataLearn.chapter_id = chapter.id;
            dataLearn.chapter_name = chapter.name
            if (typeof chapter.children != 'undefined') {
                // 目录（二级目录）
                for(var section of chapter.children){
                    dataLearn.section_id = section.id
                    dataLearn.section_name = section.name
                    if (typeof section.children != 'undefined') {
                        // 目录（三级级目录）
                        for(var subsection of section.children){
                            dataLearn.subsection_id = subsection.id
                            dataLearn.subsection_name = subsection.name
                            if (typeof subsection.children != 'undefined') {
                                console.warn("&&&&&&还有目录,暂不支持&&&&&&");
                            }else if(subsection.task_id){
                                //console.log("subsection测验"+chapter.name +" "+section.name +" "+subsection.name+"("+subsection.id+")");
                                dataLearn.task_id = subsection.task_id
                                if( subsection.finished == 1 ){
                                    // 已完成的作业进行同步
                                    uooc_push_course_chapter_task_exam ( dataLearn )
                                }
                            }else{
                                dataLearn.catalog_id = subsection.id
                                uooc_push_course_chapter_unit_learn(dataLearn);
                            }
                        }
                    }else if(section.task_id){
                        //console.log("section测验"+chapter.name +" "+section.name+"("+section.id+")");
                        dataLearn.task_id = section.task_id
                        if( section.finished == 1 ){
                            // 已完成的作业进行同步
                            uooc_push_course_chapter_task_exam ( dataLearn )
                        }
                    }else{
                        dataLearn.catalog_id = section.id
                        uooc_push_course_chapter_unit_learn(dataLearn);
                    }
                }
            }else if(chapter.task_id){
                //console.log("chapter测验"+chapter.name+"("+chapter.id+")");
                dataLearn.task_id = chapter.task_id
                if( chapter.finished == 1 ){
                    // 已完成的作业进行同步
                    uooc_push_course_chapter_task_exam ( dataLearn )
                }
            }else{
                dataLearn.catalog_id = chapter.id
                uooc_push_course_chapter_unit_learn(dataLearn);
            }
        }

    }

    function uooc_push_course_chapter_unit_learn(dataLearn){
        if(dataLearn){
            $.ajax({
                type: "GET",
                url: '/home/learn/getUnitLearn',
                data: {
                    cid: dataLearn.cid,//课程ID
                    chapter_id: dataLearn.chapter_id,//章节ID(一级目录ID)
                    section_id: dataLearn.section_id,//内容部分ID(二级目录ID)
                    subsection_id: dataLearn.subsection_id,//内容部分ID(三级目录ID)
                    catalog_id: dataLearn.catalog_id,//当前目录ID
                },
                success: function (response) {
                    if (response.code == 1) {
                        const data =response.data;
                        for (let l = 0; l < data.length; l++) {
                            if(data[l].task_id){
                                dataLearn.task_id = data[l].task_id
                                if( data[l].finished == 1){
                                    // 已完成的作业进行同步
                                    uooc_push_course_chapter_task_exam ( dataLearn )
                                }
                            }
                        }
                    }

                }
            })
        }

    }


    function uooc_push_course_chapter_task_exam(dataLearn){
        //console.log("正在同步测验==>"+dataLearn.cname+"->"+dataLearn.chapter_name+"->"+dataLearn.section_name+"->"+dataLearn.subsection_name)
        uooc_task_exam_view ( dataLearn );
    }

    // uooconline  end ...

    // Your code here...
    function urlArgs() {
        var query = window.location.search.substring(1);
       // console.log(query);
        var spArray = query.split("&");
        var args = {};
        for (var i = 0; i < spArray.length; i++) {
            var flag = spArray[i].indexOf("=");
            var key = spArray[i].substring(0, flag);
            var value = spArray[i].substring(flag + 1);
            value = decodeURIComponent(value);
            args[key] = value;
        }
        return args;
    }


    function getUrlArgValue(keyName) {
        var result = urlArgs();
        for (var key in result) {
            if(keyName == key){
                return result[key];
            }
        }
        return '';
    }


})();

